﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JEdwonTune.Crud;
using System.IO;
using System.Xml;
using JEdwonTune.Hibernate;
using JEdwonTune.Lib;
using JEdwonTune.Validation;
using JEdwonTune.Tools;
using JEdwonTune.Common;
using JEdwonTune.Jskit;

namespace JEdwonTune {
    public partial class MDIMain : Form {


        #region 原始事件处理方法
        private int childFormNumber = 0;
        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e) {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e) {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e) {
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e) {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e) {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e) {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e) {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e) {
            if (MessageBox.Show("确定要关闭吗？", "确定", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes) {
                foreach (Form childForm in MdiChildren) {
                    childForm.Close();
                }
            }
        }
        #endregion

        public MDIMain() {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }
        private void MDIMain_Load(object sender, EventArgs e) {
            this.Text = "JEdwon Tune " + MyUtil.TuneBuildVersion;
        }
        private void MDIMain_FormClosing(object sender, FormClosingEventArgs e) {
            if (MessageBox.Show("确定要关闭吗？", "确定", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) {
                e.Cancel = true;
            }
        }

        #region 捆绑tabControl
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
            foreach (FrmMDIChild childForm in this.MdiChildren) {
                //Check for its corresponding MDI child form
                if (childForm.TabPag.Equals(tabControl1.SelectedTab)) {
                    //Activate the MDI child form
                    childForm.Select();
                }
            }

        }
        private void AddMDIChildToTabCtrl(FrmMDIChild frmChild) {
            frmChild.MdiParent = this;

            //child Form will now hold a reference value to the tab control
            frmChild.TabCtrl = tabControl1;

            //Add a Tabpage and enables it
            TabPage tp = new TabPage();
            tp.Parent = tabControl1;
            tp.Text = frmChild.Text;
            tp.Show();

            //child Form will now hold a reference value to a tabpage
            frmChild.TabPag = tp;
            frmChild.Show();
        }
        private void tabControl1_MouseDown(object sender, MouseEventArgs e) {
            if (e.Button == MouseButtons.Right) {
                Rectangle rct;
                for (int i = 0; i < tabControl1.TabPages.Count; i++) {
                    rct = tabControl1.GetTabRect(i);

                    if (rct.Contains(e.X, e.Y)) {
                        //tabControl1.SelectedIndex = i;
                        tabControl1.SelectedTab = tabControl1.TabPages[i];
                        break;
                    }
                }
            }

        }
        #endregion

        #region 通用方法
        private void XmlEditorOpen(String title, XmlEditorTarget target) {
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "配置文件(*.config)|*.config|XML文件(*.xml)|*.xml|所有文件(*.*)|*.*";
            DialogResult fdr = this.openFileDialog1.ShowDialog();
            if (fdr == DialogResult.OK) {
                XmlDocument doc = new XmlDocument();
                try {
                    doc.Load(openFileDialog1.FileName);
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message, "装载文件错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    return;
                }
                FrmXmlEditor frm = new FrmXmlEditor();
                frm.MdiParent = this;
                frm.WindowState = FormWindowState.Maximized;
                frm.Text = title;
                frm.FilePath = openFileDialog1.FileName;
                frm.Doc = doc;
                frm.Target = target;
                AddMDIChildToTabCtrl(frm);
            }
        }
        private void XmlEditorCreate(String title,XmlEditorTarget target) {
            XmlDocument doc = new XmlDocument();
            if (target == XmlEditorTarget.CrudConfig) {
                doc.LoadXml(MyUtil.EmptyCrudCfg);
            } else if (target == XmlEditorTarget.VdConfig) {
                doc.LoadXml(MyUtil.EmptyVdCfg);
            } else if (target == XmlEditorTarget.Authentication) {
                doc.LoadXml(MyUtil.EmptyAuthenticationCfg);
            } else {
                return;
            }
            FrmXmlEditor frm = new FrmXmlEditor();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Text = title;
            frm.FilePath = null;
            frm.Doc = doc;
            frm.Target = target;
            AddMDIChildToTabCtrl(frm);
        }
        #endregion

        private void tsmiDbView_Click(object sender, EventArgs e) {
            FrmDbConenct fconn = new FrmDbConenct();
            DialogResult dr = fconn.ShowDialog();
            if (dr != DialogResult.OK) { return; } else {
                FrmDbView frm = new FrmDbView();
                frm.Handler = fconn.Handler;
                frm.MdiParent = this;
                frm.WindowState = FormWindowState.Maximized;
                frm.Text = "查看数据库";
                AddMDIChildToTabCtrl(frm);
            }
        }
        private void tsmiViewBizAssembly_Click(object sender, EventArgs e) {
            FrmViewBizAssembly frm = new FrmViewBizAssembly();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Text = "查看业务程序集";
            AddMDIChildToTabCtrl(frm);
        }
        private void tsmiNewCrud_Click(object sender, EventArgs e) {
            XmlEditorCreate("新建Crud配置",XmlEditorTarget.CrudConfig);
        }

        private void tsmiNewValidation_Click(object sender, EventArgs e) {
            XmlEditorCreate("新建Validation配置", XmlEditorTarget.VdConfig);
        }

        private void tsmiOpenCrud_Click(object sender, EventArgs e) {
            XmlEditorOpen("编辑Crud配置", XmlEditorTarget.CrudConfig);
        }

        private void tsmiOpenValidation_Click(object sender, EventArgs e) {
            XmlEditorOpen("编辑Validation配置", XmlEditorTarget.VdConfig);
        }

        private void tsiAbout_Click(object sender, EventArgs e) {
            FrmAbout frm = new FrmAbout();
            frm.ShowDialog();
        }

        private void tsmiPublishJskit_Click(object sender, EventArgs e) {
            if (IISHelper.CheckVirtualDirExists(MyUtil.JskitVirtualDir)) {
                MessageBox.Show("虚拟目录(" + MyUtil.JskitVirtualDir + ")已存在", "消息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (File.Exists("jskit.org.rar")) { 
                
            }
        }

        private void tsmiNewAuth_Click(object sender, EventArgs e) {
            XmlEditorCreate("新建Authentication配置", XmlEditorTarget.Authentication);
        }

        private void tmsiOpenAuth_Click(object sender, EventArgs e) {
            XmlEditorOpen("编辑Authentication配置", XmlEditorTarget.Authentication);
        }

        private void tsmiBuild_Click(object sender, EventArgs e) {
            FrmJskitBuildSrc frm = new FrmJskitBuildSrc();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Text = "Build Jskit.org";
            AddMDIChildToTabCtrl(frm);

        }

        private void tsmiOptions_Click(object sender, EventArgs e) {

        }

        private void tsmiBuildJskitPackage_Click_1(object sender, EventArgs e) {
            FrmBuildJsPackage frm = new FrmBuildJsPackage();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Text = "查看Jskit.org";
            AddMDIChildToTabCtrl(frm);
        }

        private void 生成压缩包ToolStripMenuItem_Click(object sender, EventArgs e) {
            FrmBaseZipper frm = new FrmBaseZipper();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Text = "生成Base压缩包";
            AddMDIChildToTabCtrl(frm);
        }

        private void tsmiExportMySqlViewDef_Click(object sender, EventArgs e) {

        }



    }
}
